
use std::fmt::{Display,Formatter} ; 

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum LogLevel {
    None,
    Error,
    Warn,
    Info,
    Debug,
    Trace,
}
pub static LOG_LEVEL: crate :: SyncCell<LogLevel> = crate::SyncCell::new(LogLevel::None) ;

#[inline]
pub fn env_exists(name: &str) -> bool { std::env::var_os(name).is_some_and(|s| !s.is_empty()) }


impl LogLevel {
    pub fn is_none(self) -> bool {
        self == Self::None
    }
}

impl From<String> for LogLevel {
    fn from(mut s:String) -> Self {
        s.make_ascii_uppercase();
        match s.as_str() {
            "ERROR" => LogLevel::Error,
            "WARN" => LogLevel::Warn,
            "INFO" => LogLevel::Info,
            "DEBUG" => LogLevel::Debug,
            "TRACE" => LogLevel::Trace,
            _ => LogLevel::None,
        }
    }
}
impl AsRef<str> for LogLevel {
    fn as_ref(&self) -> &str {
        match self {
            Self::None => "None" ,
            Self::Error => "ERROR",
            Self::Warn => "WARN",
            Self::Info => "INFO",
            Self::Debug => "DEBUG",
            Self::Trace => "TRACE",
        }
    }
}

impl Display for LogLevel {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f , "{}" , self.as_ref()) 
    }
}